#pragma once

#include<iostream>
using namespace std;
#include<assert.h>

namespace my
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;

		T _data;
	};

	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;

		Node* _node;

		ListIterator(Node* node = nullptr)
			:_node(node)
		{}

		ListIterator(const Self& S)
		{
			_node = S._node;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			Node* tmp = _node;
			_node = _node->_next;
			return Self(tmp);
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Node* tmp = _node;
			_node = _node->_prev;
			return Self(tmp);
		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator!=(const Self& S)
		{
			return _node != S._node;
		}

		bool operator==(const Self& S)
		{
			return _node == S._node;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;

	public:
		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}
		
		void EmptyInit()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			EmptyInit();
		}

		list(const T& val)
		{
			EmptyInit();
			push_back(val);
		}

		list(int n, const T& val = T())
		{
			EmptyInit();
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			EmptyInit();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list(const list<T>& l)
		{
			EmptyInit();
			for (auto a : l)
				push_back(a);
		}

		list<T>& operator=(list<T> l)
		{
			swap(l);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		size_t size() const
		{
			const_iterator it = begin();
			size_t size = 0;
			while (it != end())
			{
				size++;
				it++;
			}
			return size;
		}

		bool empty() const
		{
			return size() == 0;
		}

		T& front()
		{
			return _head->_next->_data;
		}

		const T& front() const
		{
			return _head->_next->_data;
		}

		T& back()
		{
			return _head->_prev->_data;
		}

		const T& back() const
		{
			return _head->_prev->_data;
		}

		iterator insert(iterator pos, const T& val)
		{
			Node* tmp = new Node;
			tmp->_data = val;
			Node* prev = pos._node->_prev;
			Node* next = pos._node;

			prev->_next = tmp;
			tmp->_next = next;
			tmp->_prev = prev;
			next->_prev = tmp;

			return iterator(tmp);
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;

			prev->_next = next;
			next->_prev = prev;

			return iterator(next);
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		void swap(list<T>& l)
		{
			std::swap(_head, l._head);
		}

		
	private:
		Node* _head;
	};

	void Test_list1()
	{
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		list<int> l2(l1);
		list<int> l3;
		l3 = l1;

		for (auto i : l1)
		{
			cout << i << ' ';
		}
		cout << endl;
		cout << l1.front() << ' ' << l1.back() << ' ' << l1.size() << endl;

		l1.push_front(5);
		l1.push_front(6);
		for (auto i : l1)
		{
			cout << i << ' ';
		}
		cout << endl;
		cout << l1.front() << ' ' << l1.back() << ' ' << l1.size() << endl;

		l1.pop_back();
		l1.pop_back();
		for (auto i : l1)
		{
			cout << i << ' ';
		}
		cout << endl;
		cout << l1.front() << ' ' << l1.back() << ' ' << l1.size() << endl;

		l1.pop_front();
		l1.pop_front();
		for (auto i : l1)
		{
			cout << i << ' ';
		}
		cout << endl;
		cout << l1.front() << ' ' << l1.back() << ' ' << l1.size() << endl;
		
		cout << "l2: ";
		for (auto i : l2)
		{
			cout << i << ' ';
		}
		cout << endl;

		cout << "l3: ";
		for (auto i : l3)
		{
			cout << i << ' ';
		}
		cout << endl;
	}
}